package com.javaex.book.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限管理
 *
 * @author 黄星琦
 * @Date 2022年3月1日
 */
@WebFilter(urlPatterns = {
	"/manager/*",
	"/order/*",
	"/user/detail"
})
public class ManagerFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		Object loginUser = req.getSession().getAttribute("loginUser");
		if (loginUser == null) {
			HttpServletResponse resp = (HttpServletResponse) response;
			resp.sendRedirect(req.getContextPath() + "/user/login");
			return;
		}
		chain.doFilter(request, response);
	}

}
